package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseSaleAttr;
import com.atguigu.gmall.product.model.SpuImage;
import com.atguigu.gmall.product.model.SpuInfo;
import com.atguigu.gmall.product.model.SpuSaleAttr;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2022-12-24 14:11
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManageController {


    @Autowired
    private SpuManageService spuManageService;


    /**
     * spu分页列表
     *
     * @param page
     * @param size
     * @param spuInfo
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result<IPage<SpuInfo>> getSpuInfoPage(
            @PathVariable("page") Long page,
            @PathVariable("size") Long size,
            SpuInfo spuInfo) {
        //1.构建分页对象
        IPage<SpuInfo> iPage = new Page<>(page, size);
        //2.调用业务逻辑
        iPage = spuManageService.getSpuInfoPage(iPage, spuInfo);
        return Result.ok(iPage);
    }

    /**
     * 加载平台中所有的销售属性名称列表
     *
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result<List<BaseSaleAttr>> baseSaleAttrList() {
        List<BaseSaleAttr> list = spuManageService.baseSaleAttrList();
        return Result.ok(list);
    }


    /**
     * 保存spu
     *
     * @param spuInfo
     * @return
     */
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        // 调用服务层的保存方法
        spuManageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }


    /**
     * 根据商品SPUID查询销售属性名称以及值
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrList(spuId);
        return Result.ok(list);
    }

    /**
     * 根据商品SPUID查询商品Spu图片列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImageList(@PathVariable("spuId") Long spuId){
        List<SpuImage> list = spuManageService.getSpuImageList(spuId);
        return Result.ok(list);
    }
}
